// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Linq.Expressions;
using System.Reflection;
using SnapObjects.Commons;

namespace SnapObjects.Data
{
    internal class ModelHelper
    {
        public static bool IsDynamicModel(Type modelType)
        {
            return typeof(DynamicModel).IsAssignableFrom(modelType);
        }

        public static bool IsValueEquals(object value1, object value2)
        {
            if (value1 == null && value2 == null)
            {
                return true;
            }

            if (value1 == null || value2 == null)
            {
                return false;
            }

            if (value1.GetType() != value2.GetType())
            {
                return false;
            }

            return value1.Equals(value2);
        }

        public static PropertyInfo ExtractProperty<TModel>(Expression<Func<TModel, object>> property)
        {
            if (property == null)
            {
                return null;
            }

            var propertyInfo = property.ExtractProperty();
            if (propertyInfo == null)
            {
                throw new InvalidOperationException("Selected member is not a property");
            }

            return propertyInfo;
        }
    }
}
